package com.lyz.collectionDemo.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @ClassName GameDemo
 * @Description collection集合 斗地主案例
 * @Author 2214361854
 * @Date 2022/2/22 19:54
 * @Version 1.0
 */
public class GameDemo {
    /**
     * 静态集合存54张牌对象
     */
    public static List<Card> allCards = new ArrayList<>();

    static{
        String[] sizes = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♥","♢","♠","♣"};
        int index = 0;
        for (String size : sizes) {
            index++;
            for (String color : colors) {
                Card card = new Card(size,color,index);
                allCards.add(card);
            }
        }
        Card c1 = new Card("","小王",++index);
        Card c2 = new Card("","大王",++index);
        Collections.addAll(allCards,c1,c2);
        System.out.println("新牌："+allCards);
    }
    public static void main(String[] args) {
        //洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌："+allCards);

        //玩家
        List<Card> ah = new ArrayList<>();
        List<Card> bh = new ArrayList<>();
        List<Card> ch = new ArrayList<>();

        //发牌
        for (int i = 0; i < allCards.size() - 3; i++) {
            Card card = allCards.get(i);
            if (i%3==0){
                ah.add(card);
            } else if (i % 3 == 1) {
                bh.add(card);
            }else if (i % 3 == 2) {
                ch.add(card);
            }
        }

        //地主牌截取3张
        List<Card> lastThreeCards = allCards.subList(allCards.size()-3,allCards.size());

        sortCards(ah);
        sortCards(bh);
        sortCards(ch);
        sortCards(lastThreeCards);

        //玩家排序
        System.out.println("上家"+ah);
        System.out.println("本家"+bh);
        System.out.println("下家"+ch);
        System.out.println("地主牌"+lastThreeCards);
    }

    /**
     * 玩家牌排序
     * @param cards
     */
    private static void sortCards(List<Card> cards) {
            Collections.sort(cards, (o2,o1) -> o2.getIndex() - o1.getIndex());
    }
}
